package cn.lili.modules.order.order.serviceimpl;

import cn.hutool.core.util.StrUtil;
import cn.lili.modules.order.order.entity.dos.UserFreetaxOrderYearTotal;
import cn.lili.modules.order.order.mapper.UserFreetaxOrderYearTotalMapper;
import cn.lili.modules.order.order.service.UserFreetaxOrderYearTotalService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户免税订单年交易量 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-11
 */
@Service
public class UserFreetaxOrderYearTotalServiceImpl extends ServiceImpl<UserFreetaxOrderYearTotalMapper, UserFreetaxOrderYearTotal> implements UserFreetaxOrderYearTotalService {


    /**
     * 根据身份证号码查询年度免税订单
     *
     * @param buyerCardNo
     * @return
     */
    @Override
    public UserFreetaxOrderYearTotal selectUserFreeTaxTotalByIdCardNo(String buyerCardNo,Integer yearDate) {
        LambdaQueryWrapper<UserFreetaxOrderYearTotal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StrUtil.isNotEmpty(buyerCardNo), UserFreetaxOrderYearTotal::getBuyerCardNo, buyerCardNo);
        queryWrapper.eq(yearDate != null, UserFreetaxOrderYearTotal::getYearDate, yearDate);
        return baseMapper.selectOne(queryWrapper);
    }


    @Override
    public int updateOrSaveUserFree(UserFreetaxOrderYearTotal userFree) {
        UserFreetaxOrderYearTotal userFreeData = this.selectUserFreeTaxTotalByIdCardNo(userFree.getBuyerCardNo(), userFree.getYearDate());
        if (userFreeData == null) {
            return baseMapper.insert(userFree);
        } else {
            userFree.setId(userFreeData.getId());
            return baseMapper.updateUserFreeOrderYearTotal(userFree);
        }
    }


}
